**************************************************************************************
** title: 		Transformation of the political space: A Citizens’ perspective	 	**
** authors:		dassonneville, hooghe & marks 										**
** purpose: 	replicating analyses in manuscript & SM								**
** date:		February 2023 														**
** machine:		macOS Big Sur 11.3.1 												**
** software:	Stata 16.0 															**	
**************************************************************************************


* -------------
* Preamble 
* -------------

	** install packages (uncomment if not installed yet)
	*ssc install vce2way
	*ssc install heatplot

	** set scheme for graphs
	grstyle init
	grstyle set plain, nogrid noextend

	** set working directory (update working directory as a function of where you store the replication materials and datasets)
	cd "~/Dropbox/transformation of the political space-replication/"

	** pool the 1999, 2009, 2014 and 2019 data
	use   "data/ees-votersurvey-1999-coded-r&r.dta", clear
	append using "data/ees-votersurvey-2009-coded-r&r.dta"
	append using "data/ees-votersurvey-2014-coded-r&r.dta"
	append using "data/ees-votersurvey-2019-coded-r&r.dta"
	
	replace respid=var001 if year==1999 		// 1999 data had a different respondent id variable name
	replace countryweight=1 if countryweight==.	// 1999 data have a country-weight, give weight of 1 to observations of all other years
	
* -------------------------------------------------------------------------------------------------	
* Figure 1, Heatplot that shows the average PTV by electorate-party combination, Germany 2019
* -------------------------------------------------------------------------------------------------	

	preserve 
	
	use "data/ZA7581_v2-0-1.dta", clear
	keep if hCountry==8
	
	recode Q7n (0=.) (20=.) (90=.) (95=.) (96=.) (98=.) (99=.)
	recode q10_1 q10_2 q10_3 q10_4 q10_5 q10_6 q10_7  (0=.) (20=.) (90=.) (95=.) (96=.) (98=.) (99=.)
	drop if Q7n==.
	foreach var of varlist q10_1 q10_2 q10_3 q10_4 q10_5 q10_6 q10_7  {
	bys Q7n: egen sd_`var' = sd(`var')
	}
	collapse q10_1 q10_2 q10_3 q10_4 q10_5 q10_6 q10_7 , by(Q7n)
	mkmat q10_1 q10_2 q10_3 q10_4 q10_5 q10_6 q10_7 , matrix(therm)
	display `therm'
	restore
	
	matrix list therm
	heatplot therm, cuts(0(1)10)  size  ///
	ylabel(1 "{bf:CDU/CSU}" 2 "{bf:SPD}" 3 "{bf:FDP}" 4 "{bf:Grüne}" 5 "{bf:Die Linke}" 6 "{bf:AfD}" 7 "{bf:Piraten}") ///
	xlabel(1 "{bf:CDU/CSU}" 2 "{bf:SPD}" 3 "{bf:FDP}" 4 "{bf:Grüne}" 5 "{bf:Die Linke}" 6 "{bf:AfD}" 7 "{bf:Piraten}" ) ///
	xtitle("") ytitle("") legend(title("")) xsize(6.5) ramp(right space(45) labels(0 "Not at all probable" 10 "Very probable") 	subtitle("")) ///
	values(format(%9.1f) mlabsize(vsmall)) ///
	ytitle("Party voted for") xtitle("PTV for party...") 
	graph export "heatplot-deu-2019.pdf", replace 		
	
	
	
* ------------------------------------------------------------------------------------------------------------
* Table 1, Explaining PTV with ideological distance between party voted and party evaluated, pooled data
* ------------------------------------------------------------------------------------------------------------
	
	* estimate models
	eststo distance1: vce2way reg ptv distance_lrecon  i.country i.year if votedforparty!=1 [aweight=countryweight], cluster(respid country)
	eststo distance2: vce2way reg ptv  distance_galtan i.country i.year if votedforparty!=1 [aweight=countryweight], cluster(respid country)
	eststo distance3: vce2way reg ptv distance_lrecon distance_galtan i.country i.year if votedforparty!=1 [aweight=countryweight], cluster(respid country)
	
	* output tables, in rtf and tex format
	esttab distance1 distance2 distance3 using pooled.rtf, cells(b(star fmt(3)) se(par fmt(3)))  collab(none) stats(N r2 , fmt(0 3)) legend numbers  drop (*.country *.year) nogap replace
	esttab distance1 distance2 distance3 using pooled.tex, cells(b(star fmt(3)) se(par fmt(3)))  collab(none) stats(N r2 , fmt(0 3)) legend numbers  drop (*.country *.year) nogap replace
	
	
* ------------------------------------------------------------------------
* Figure 2. Average marginal effect of distance on PTVs, change over time
* ------------------------------------------------------------------------
	
	* create 'wave' variables for plotting estimates at equal distance on x-axis
	gen wave=1 if year==1999
	replace wave=2 if year==2009
	replace wave=3 if year==2014
	replace wave=4 if year==2019
	
	* estimate main model
	eststo change1: vce2way reg ptv i.wave##c.distance_lrecon i.wave##c.distance_galtan i.country if votedforparty!=1 [aweight=countryweight], cluster(respid country)	
	
	* Figure 2, panel a
	margins , dydx(distance_lrecon) at(wave=(1 2 3 4 ))
	marginsplot , title("") recast(scatter) recastci(rspike) yline(0, lpattern(dash)) xtitle("") xlabel(1 "1999" 2 "2009" 3 "2014" 4 "2019") ylabel(-0.4(0.2)0.2) title("") plotopts(msymbol(O) msize(medlarge)) saving(ecotrend.gph, replace)
	
	* Figure 2, panel b
	margins , dydx(distance_galtan) at(wave=(1 2 3 4))
	marginsplot, title("") recast(scatter) recastci(rspike) yline(0, lpattern(dash)) xtitle("") xlabel(1 "1999" 2 "2009" 3 "2014" 4 "2019") ylabel(-0.4(0.2)0.2) title("") plotopts(msymbol(O) msize(medlarge))  saving(galtantrend.gph, replace)
	
	* output in tabular format (included in Appendix H)
	esttab  change1 using spatialchange.rtf, cells(b(star fmt(3)) se(par fmt(3)))  collab(none) stats(N r2 , fmt(0 3)) legend numbers  drop (*.country ) nogap replace
	esttab  change1 using spatialchange.tex, cells(b(star fmt(3)) se(par fmt(3)))  collab(none) stats(N r2 , fmt(0 3)) legend numbers  drop (*.country ) nogap replace
	
	
* -------------------------------------------------------------		
* Figure 3. Predicted PTV by party family dyad (2019 data)	
* -------------------------------------------------------------	

	** code additional variables for the analyses: party family dyads
	decode family_voted, gen(family_voted_string)
	decode family, gen(family_string)
	gen party_dyad=family_voted_string+"_"+family_string
	replace party_dyad="" if family_voted==.
	replace party_dyad="" if family==.
	drop if party_dyad==""
	replace party_dyad="" if family_voted==family		// drop within-family dyads
	encode party_dyad, gen(party_dyad_num)

	** arrange such that rad right is the first leg || next, green first leg
	gen party_dyad_lim=party_dyad	
	replace party_dyad_lim="tan_cons" if party_dyad=="cons_tan" 
	replace party_dyad_lim="tan_liberal" if party_dyad=="liberal_tan"
	replace party_dyad_lim="tan_christdem" if party_dyad=="christdem_tan" 
	replace party_dyad_lim="tan_socialist" if party_dyad=="socialist_tan"
	replace party_dyad_lim="tan_rad left" if party_dyad=="rad left_tan"
	replace party_dyad_lim="tan_green" if party_dyad=="green_tan"
	replace party_dyad_lim="green_cons" if party_dyad=="cons_green"
	replace party_dyad_lim="green_liberal" if party_dyad=="liberal_green"
	replace party_dyad_lim="green_christdem" if party_dyad=="christdem_green"
	replace party_dyad_lim="green_socialist" if party_dyad=="socialist_green"
	replace party_dyad_lim="green_rad left" if party_dyad=="rad left_green"
	replace party_dyad_lim="christdem_cons" if party_dyad=="cons_christdem"
	replace party_dyad_lim="christdem_liberal" if party_dyad=="liberal_christdem"
	replace party_dyad_lim="christdem_socialist" if party_dyad=="socialist_christdem"
	replace party_dyad_lim="christdem_rad left" if party_dyad=="rad left_christdem"
	replace party_dyad_lim="cons_liberal" if party_dyad=="liberal_cons"
	replace party_dyad_lim="cons_socialist" if party_dyad=="socialist_cons"
	replace party_dyad_lim="cons_rad left" if party_dyad=="rad left_cons"
	replace party_dyad_lim="liberal_rad left" if party_dyad=="rad left_liberal"
	replace party_dyad_lim="liberal_socialist" if party_dyad=="socialist_liberal"
	replace party_dyad_lim="rad left_socialist" if party_dyad=="socialist_rad left"

	tab party_dyad_lim
	replace party_dyad_lim="" if family_voted==family		// drop within-family dyads
	encode party_dyad_lim, gen(party_dyad_lim_num)
	tab party_dyad_lim_num, nolabel
	
	** main analysis: focus on reduced form of dyads (combining different directions, e.g., cons-green = green-cons, year-specific analyses 
	eststo dyad2019: vce2way reg ptv b15.party_dyad_lim_num i.country if family_voted!=. & year==2019, cluster(respid country)
	margins party_dyad_lim_num , saving("dyadmargins2019", replace)
	
	** create Figure 3
	preserve
	use "dyadmargins2019", clear
	sort _margin 
	gen obsno = _n 
	labmask obsno, values(_m1) lblname(_m1) decode
	twoway scatter obsno _margin, yla(1/21, valuelabel  noticks) msymbol(O) msize(medlarge)  || rspike _ci_lb _ci_ub obsno, horizontal xtitle("Predicted PTV") ytitle("") xlabel(0(1)6) legend(off) title("")  saving(ptvbydyad_2019.gph, replace)
	restore
	
	** output in tabular format (included in Appendix I)
	esttab dyad2019 using appendix_dyadestimates.tex, cells(b(star fmt(3)) se(par fmt(3)))  collab(none) stats(N r2 aic bic, fmt(0 3)) legend numbers  drop (*.country ) nogap replace
	
	
* -----------------------------------------------------------------------------------	
* Figure 4. Predicted vote propensity (PTV) for party families and their voters 
* -----------------------------------------------------------------------------------	

	** obtain average by year (for reference line)
	vce2way reg ptv i.family i.country i.year if votedforparty!=1 & family_voted!=. [aweight=countryweight], cluster(respid country)
	sum ptv if e(sample)==1	& year==1999	// obtain average for reference line on 1999 plot (= 2.30)
	sum ptv if e(sample)==1	& year==2009	// obtain average for reference line on 2009 plot (= 2.72)
	sum ptv if e(sample)==1	& year==2014	// obtain average for reference line on 2014 plot (= 2.58)
	sum ptv if e(sample)==1	& year==2019	// obtain average for reference line on 2019 plot (= 2.87)

	** Figure 4, panel a, TAN as object: how do voters evaluate parties of different party families
	vce2way reg ptv i.family i.country  if votedforparty!=1 & family_voted!=. & year==2019, cluster(respid country)
	margins family
	marginsplot, xtitle("Party family that is evaluated") ytitle("Predicted PTV") title("") recast(scatter) plot1opts(msymbol(O)) recastci(rspike) yline(2.87, lpattern(dash)) xlabel(, labsize(small) ) ylabel(1(1)4) title("2019 EES") plotopts(msymbol(O) msize(medlarge))  saving(tanobject_2019.gph, replace)
	
	** Figure 4, panel b, TAN as subject: how does a party family constituency evaluate others party families 		
	vce2way reg ptv i.family_voted i.country  if votedforparty!=1 & year==2019 , cluster(respid country)
	margins family_voted
	marginsplot, xtitle("Voted for party of party family...") ytitle("Predicted PTV other parties") title("") recast(scatter) plot1opts(msymbol(O)) recastci(rspike) yline(2.87, lpattern(dash)) xlabel(, labsize(small) ) ylabel(1.5(0.5)4.5) title("2019 EES") plotopts(msymbol(O) msize(medlarge)) saving(tansubject_2019.gph, replace)		
	
	
* -------------------------------------------------------------------------------------------------------------------------------------		
* Figure 5. Change in the coefficients of party family dyads when accounting for economic left-right and GAL-TAN distance, 1999 EES
* -------------------------------------------------------------------------------------------------------------------------------------	

	use "data/coefreduced-1999.dta", clear
	
	twoway dropline amediff1 partydyadnum   , horizontal ylabel(1(1)20, valuelabel) xsize(4) ysize(6) 	xtitle("Reduction in party dyad coefficient") ytitle("") xlabel(-0.5(0.5)2) saving(coef1-1999.gph, replace)
	twoway dropline amediff2 partydyadnum   , horizontal ylabel(1(1)20, valuelabel) xsize(4) ysize(6) xtitle("Reduction in party dyad coefficient") ytitle("") xlabel(-0.5(0.5)2) saving(coef2-1999.gph, replace)
	twoway dropline amediff3 partydyadnum   , horizontal ylabel(1(1)20, valuelabel) xsize(4) ysize(6) xtitle("Reduction in party dyad coefficient") ytitle("") xlabel(-0.5(0.5)2) saving(coef3-1999.gph, replace)
	

* -------------------------------------------------------------------------------------------------------------------------------------	
* Figure 6. Change in the coefficients of party family dyads when accounting for economic left-right and GAL-TAN distance, 2019 EES	
* -------------------------------------------------------------------------------------------------------------------------------------	

	use "data/coefreduced-2019.dta", clear		
	
	twoway dropline amediff1 partydyadnum   , horizontal ylabel(1(1)20, valuelabel) xsize(4) ysize(6) 	xtitle("Reduction in party dyad coefficient") ytitle("") xlabel(-0.5(0.5)2) saving(coef1.gph, replace)
	twoway dropline amediff2 partydyadnum   , horizontal ylabel(1(1)20, valuelabel) xsize(4) ysize(6) xtitle("Reduction in party dyad coefficient") ytitle("") xlabel(-0.5(0.5)2) saving(coef2.gph, replace)
	twoway dropline amediff3 partydyadnum   , horizontal ylabel(1(1)20, valuelabel) xsize(4) ysize(6) xtitle("Reduction in party dyad coefficient") ytitle("") xlabel(-0.5(0.5)2) saving(coef3.gph, replace)

	
* -----------------------------------------------------------------------------
* Supplementary materials based on EES data (everything except for Appendix P)	
* -----------------------------------------------------------------------------


** Table A.1: Principal Components Analysis of items underlying GAL-TAN

	use "data/1999-2019_CHES_dataset_means(v3).dta", clear
	
		* identify which items are available each year
	sum sociallifestyle immigrate_policy multiculturalism nationalism environment if year==2006 
	sum sociallifestyle immigrate_policy multiculturalism nationalism environment if year==2010
	sum sociallifestyle immigrate_policy multiculturalism nationalism environment if year==2014
	sum sociallifestyle immigrate_policy multiculturalism nationalism environment if year==2019
	
	
		* PCA analysis 2006
	pca sociallifestyle immigrate_policy multiculturalism if year==2006 & eastwest==1
	predict component2006
		
		* PCA analysis 2010
	pca sociallifestyle immigrate_policy multiculturalism environment if year==2010 & eastwest==1
	predict component2010	
		
		* PCA analysis 2014
	pca sociallifestyle immigrate_policy multiculturalism nationalism environment if year==2014 & eastwest==1
	predict component2014		
		
		* PCA analysis 2019
	pca sociallifestyle immigrate_policy multiculturalism nationalism environment if year==2019 & eastwest==1
	predict component2019		

		* correlation between PCA indices and GAL-TAN measure (shown in bottom row of table A.1)
	pwcorr component2006 galtan if year==2006
	pwcorr component2010 galtan if year==2010
	pwcorr component2014 galtan if year==2014
	pwcorr component2019 galtan	if year==2019


** Table B.1: Elections included in analysis of CSES IMD dataset
	
	* load data
	use "data/cses-imd-coded.dta", clear
	
	* label party family variables
	label define familylabel 1 "tan" 2 "cons" 3 "liberal" 4 "christdem" 5 "socialist" 6 "rad left" 7 "green" 
	label values family familylabel 
	label values family_voted familylabel 
	label values family familylabel
	
	* elections included
	tab IMD1004
	
** Table B.2: Explaining like/dislike ratings with information on party voted for and party that is evaluated, full results	

	eststo family1: vce2way reg LikeDislike i.family_voted i.country if votedforparty!=1, cluster(id_num country)	
	eststo family2: vce2way reg LikeDislike i.family i.country if votedforparty!=1 & family_voted!=., cluster(id_num country)
	esttab family1 family2 using appendix_cses_families.tex, cells(b(star fmt(3)) se(par fmt(3))) starlevels(* 0.05 ** 0.01 *** 0.001) collab(none) stats(N r2, fmt(0 3)) legend numbers  drop () nogap replace	


** Figure B.1: Predicted like/dislike rating by party family voted for
	
	eststo family1: vce2way reg LikeDislike i.family_voted i.country if votedforparty!=1, cluster(id_num country)
	sum LikeDislike if e(sample)==1		// obtain average for reference line on plot (= 3.91)
	vce2way reg LikeDislike i.family_voted i.country if votedforparty!=1, cluster(id_num country)
	margins family_voted
	marginsplot, xtitle("Voted for party of party family...") ytitle("Predicted like/dislike rating other parties") title("") recast(scatter) plot1opts(msymbol(O)) recastci(rspike) yline(3.91, lpattern(dash)) xlabel(, labsize(small) ) ylabel(3(1)5) plotopts(msymbol(O) msize(medlarge))  saving(appendix_csesfamilies1.gph, replace)


** Figure B.2: Predicted like/dislike rating by party family that is evaluated

	eststo family2: vce2way reg LikeDislike i.family i.country if votedforparty!=1 & family_voted!=., cluster(id_num country)
	sum LikeDislike if e(sample)==1		// obtain average for reference line on plot (= 3.96)
	vce2way reg LikeDislike i.family i.country if votedforparty!=1 & family_voted!=., cluster(id_num country)
	margins family
	marginsplot, xtitle("Party family of the party that is evaluated") ytitle("Predicted like/dislike rating") title("") recast(scatter) plot1opts(msymbol(O)) recastci(rspike) yline(3.96, lpattern(dash)) xlabel(, labsize(small) ) ylabel(2(1)6) plotopts(msymbol(O) msize(medlarge))  saving(appendix_csesfamilies2.gph, replace)


** Table B.3: Explaining like/dislike ratings by means of information on ideological distance between party voted for and the party that is evaluated, CSES IMD data

	eststo distance1: vce2way reg LikeDislike distance_lrgen i.country if  votedforparty!=1 , cluster(id_num country)
	eststo distance2: vce2way reg LikeDislike distance_lrecon distance_galtan i.country if  votedforparty!=1, cluster(id_num country)
	eststo distance3: vce2way reg LikeDislike c.distance_lrecon##c.distance_lrecon c.distance_galtan##c.distance_galtan i.country if  votedforparty!=1, cluster(id_num country)
	
	esttab distance1 distance2 distance3 using appendix_cses_distance.tex, cells(b(star fmt(3)) se(par fmt(3))) starlevels(* 0.05 ** 0.01 *** 0.001) collab(none) stats(N r2, fmt(0 3)) legend numbers  drop () nogap replace

		
** Table E.1: Explaining PTV with ideological distance between party voted and party evaluated, pooled data – focus on national party vote

	* load and pool data
	use   "data/ees-votersurvey-1999-coded-r&r.dta", clear
	append using "data/ees-votersurvey-2009-coded-r&r.dta"
	append using "data/ees-votersurvey-2014-coded-r&r.dta"
	append using "data/ees-votersurvey-2019-coded-r&r.dta"
	replace respid=var001 if year==1999 		// 1999 data had a different respondent id variable name
	replace countryweight=1 if countryweight==.	// 1999 data have a country-weight, give weight of 1 to observations of all other years

	* estimation	
	eststo distance1: vce2way reg ptv distance_lreconnat  i.country i.year if votedforpartynat!=1 [aweight=countryweight], cluster(respid country)
	eststo distance2: vce2way reg ptv  distance_galtannat i.country i.year if votedforpartynat!=1 [aweight=countryweight], cluster(respid country)
	eststo distance3: vce2way reg ptv distance_lreconnat distance_galtannat i.country i.year if votedforpartynat!=1 [aweight=countryweight], cluster(respid country)
	
	* table output
	esttab distance1 distance2 distance3 using pooled_national.tex, cells(b(star fmt(3)) se(par fmt(3)))  collab(none) stats(N r2 , fmt(0 3)) legend numbers  drop (*.country *.year) nogap replace
	
	
** Figure E.1: Average marginal effect of distance on PTVs, change over time – focus on national party vote	

	* create 'wave' variables for plotting estimates at equal distance on x-axis
	gen wave=1 if year==1999
	replace wave=2 if year==2009
	replace wave=3 if year==2014
	replace wave=4 if year==2019
	
	* estimation
	eststo change1: vce2way reg ptv i.wave##c.distance_lreconnat i.wave##c.distance_galtannat i.country if votedforpartynat!=1 [aweight=countryweight], cluster(respid country)	
	
	* Figure E.1, panel a
	margins , dydx(distance_lreconnat) at(wave=(1 2 3 4 ))
	marginsplot , title("") recast(scatter) recastci(rspike) yline(0, lpattern(dash)) xtitle("") xlabel(1 "1999" 2 "2009" 3 "2014" 4 "2019") ylabel(-0.4(0.2)0.2) title("") plotopts(msymbol(O) msize(medlarge))  saving(ecotrendnat.gph, replace)
	
	* Figure E.1, panel b
	margins , dydx(distance_galtannat) at(wave=(1 2 3 4))
	marginsplot, title("") recast(scatter) recastci(rspike) yline(0, lpattern(dash)) xtitle("") xlabel(1 "1999" 2 "2009" 3 "2014" 4 "2019") ylabel(-0.4(0.2)0.2) title("") plotopts(msymbol(O) msize(medlarge))  saving(galtantrendnat.gph, replace)
	
	* tabular format 
	esttab  change1 using spatialchangenat.tex, cells(b(star fmt(3)) se(par fmt(3)))  collab(none) stats(N r2 , fmt(0 3)) legend numbers  drop (*.country ) nogap replace

** Figure E.2: Predicted PTV by party family dyad – focus on national party vote
	
	decode family_voted, gen(family_voted_string)
	decode family, gen(family_string)
	
	decode family_votednat, gen(family_votednat_string)
	gen party_dyadnat=family_votednat_string+"_"+family_string
	replace party_dyadnat="" if family_votednat==.
	replace party_dyadnat="" if family==.
	drop if party_dyadnat==""
	replace party_dyadnat="" if family_votednat==family		// drop within-family dyads
	encode party_dyadnat, gen(party_dyadnat_num)

	gen party_dyadnat_lim=party_dyadnat	
	replace party_dyadnat_lim="tan_cons" if party_dyadnat=="cons_tan" 
	replace party_dyadnat_lim="tan_liberal" if party_dyadnat=="liberal_tan"
	replace party_dyadnat_lim="tan_christdem" if party_dyadnat=="christdem_tan" 
	replace party_dyadnat_lim="tan_socialist" if party_dyadnat=="socialist_tan"
	replace party_dyadnat_lim="tan_rad left" if party_dyadnat=="rad left_tan"
	replace party_dyadnat_lim="tan_green" if party_dyadnat=="green_tan"
	replace party_dyadnat_lim="green_cons" if party_dyadnat=="cons_green"
	replace party_dyadnat_lim="green_liberal" if party_dyadnat=="liberal_green"
	replace party_dyadnat_lim="green_christdem" if party_dyadnat=="christdem_green"
	replace party_dyadnat_lim="green_socialist" if party_dyadnat=="socialist_green"
	replace party_dyadnat_lim="green_rad left" if party_dyadnat=="rad left_green"
	replace party_dyadnat_lim="christdem_cons" if party_dyadnat=="cons_christdem"
	replace party_dyadnat_lim="christdem_liberal" if party_dyadnat=="liberal_christdem"
	replace party_dyadnat_lim="christdem_socialist" if party_dyadnat=="socialist_christdem"
	replace party_dyadnat_lim="christdem_rad left" if party_dyadnat=="rad left_christdem"
	replace party_dyadnat_lim="cons_liberal" if party_dyadnat=="liberal_cons"
	replace party_dyadnat_lim="cons_socialist" if party_dyadnat=="socialist_cons"
	replace party_dyadnat_lim="cons_rad left" if party_dyadnat=="rad left_cons"
	replace party_dyadnat_lim="liberal_rad left" if party_dyadnat=="rad left_liberal"
	replace party_dyadnat_lim="liberal_socialist" if party_dyadnat=="socialist_liberal"
	replace party_dyadnat_lim="rad left_socialist" if party_dyadnat=="socialist_rad left"

	tab party_dyadnat_lim
	replace party_dyadnat_lim="" if family_votednat==family		// drop within-family dyads
	encode party_dyadnat_lim, gen(party_dyadnat_lim_num)
	tab party_dyadnat_lim_num, nolabel

	eststo dyad2019: vce2way reg ptv b15.party_dyadnat_lim_num i.country if family_votednat!=. & year==2019, cluster(respid country)
	margins party_dyadnat_lim_num , saving("dyadmargins2019nat", replace)

	preserve
	use "dyadmargins2019nat", clear
	sort _margin 
	gen obsno = _n 
	labmask obsno, values(_m1) lblname(_m1) decode
	twoway scatter obsno _margin, yla(1/21, valuelabel  noticks) msymbol(O) msize(medlarge)  || rspike _ci_lb _ci_ub obsno, horizontal xtitle("Predicted PTV") ytitle("") xlabel(0(1)6) legend(off) title("") saving(ptvbydyadnat_2019.gph, replace)
	restore

	
** Table F.1: Explaining PTV with ideological distance between party of partisanship and party evaluated, pooled data

	* load and pool data
	use   "data/ees-votersurvey-1999-coded-r&r.dta", clear
	append using "data/ees-votersurvey-2009-coded-r&r.dta"
	append using "data/ees-votersurvey-2014-coded-r&r.dta"
	append using "data/ees-votersurvey-2019-coded-r&r.dta"
	replace respid=var001 if year==1999 		// 1999 data had a different respondent id variable name
	replace countryweight=1 if countryweight==.	// 1999 data have a country-weight, give weight of 1 to observations of all other years
	
	* create 'wave' variables for plotting estimates at equal distance on x-axis
	gen wave=1 if year==1999
	replace wave=2 if year==2009
	replace wave=3 if year==2014
	replace wave=4 if year==2019

	* estimation 
	eststo distance1: vce2way reg ptv distance_lreconpid  i.country i.year if pidparty!=1 [aweight=countryweight], cluster(respid country)
	eststo distance2: vce2way reg ptv  distance_galtanpid i.country i.year if pidparty!=1 [aweight=countryweight], cluster(respid country)
	eststo distance3: vce2way reg ptv distance_lreconpid distance_galtanpid i.country i.year if pidparty!=1 [aweight=countryweight], cluster(respid country)
	
	* output table 
	esttab distance1 distance2 distance3 using pooled_pid.tex, cells(b(star fmt(3)) se(par fmt(3)))  collab(none) stats(N r2 , fmt(0 3)) legend numbers  drop (*.country *.year) nogap replace

	
** Figure F.1: Average marginal effect of distance on PTVs, change over time – focus on partisanship

	* estimation 		
	eststo change1: vce2way reg ptv i.wave##c.distance_lreconpid i.wave##c.distance_galtanpid i.country if pidparty!=1 [aweight=countryweight], cluster(respid country)	
	
	* Figure F.1., panel a
	margins , dydx(distance_lreconpid) at(wave=(1 2 3 4 ))
	marginsplot , title("") recast(scatter) recastci(rspike) yline(0, lpattern(dash)) xtitle("") xlabel(1 "1999" 2 "2009" 3 "2014" 4 "2019") ylabel(-0.4(0.2)0.2) title("") plotopts(msymbol(O) msize(medlarge))  saving(ecotrendpid.gph, replace)
	
	* Figure F.1., panel b
	margins , dydx(distance_galtanpid) at(wave=(1 2 3 4))
	marginsplot, title("") recast(scatter) recastci(rspike) yline(0, lpattern(dash)) xtitle("") xlabel(1 "1999" 2 "2009" 3 "2014" 4 "2019") ylabel(-0.4(0.2)0.2) title("") plotopts(msymbol(O) msize(medlarge))  saving(galtantrendpid.gph, replace)
	
	* output table 
	esttab  change1 using spatialchangepid.tex, cells(b(star fmt(3)) se(par fmt(3)))  collab(none) stats(N r2 , fmt(0 3)) legend numbers  drop (*.country ) nogap replace
	
** Figure F.2: Predicted PTV by party family dyad – focus on partisanship
	
	decode family, gen(family_string)
	decode family_pid, gen(family_pid_string)
	gen party_dyadpid=family_pid_string+"_"+family_string
	replace party_dyadpid="" if family_pid==.
	replace party_dyadpid="" if family==.
	drop if party_dyadpid==""
	replace party_dyadpid="" if family_pid==family		// drop within-family dyads
	encode party_dyadpid, gen(party_dyadpid_num)

	gen party_dyadpid_lim=party_dyadpid	
	replace party_dyadpid_lim="tan_cons" if party_dyadpid=="cons_tan" 
	replace party_dyadpid_lim="tan_liberal" if party_dyadpid=="liberal_tan"
	replace party_dyadpid_lim="tan_christdem" if party_dyadpid=="christdem_tan" 
	replace party_dyadpid_lim="tan_socialist" if party_dyadpid=="socialist_tan"
	replace party_dyadpid_lim="tan_rad left" if party_dyadpid=="rad left_tan"
	replace party_dyadpid_lim="tan_green" if party_dyadpid=="green_tan"
	replace party_dyadpid_lim="green_cons" if party_dyadpid=="cons_green"
	replace party_dyadpid_lim="green_liberal" if party_dyadpid=="liberal_green"
	replace party_dyadpid_lim="green_christdem" if party_dyadpid=="christdem_green"
	replace party_dyadpid_lim="green_socialist" if party_dyadpid=="socialist_green"
	replace party_dyadpid_lim="green_rad left" if party_dyadpid=="rad left_green"
	replace party_dyadpid_lim="christdem_cons" if party_dyadpid=="cons_christdem"
	replace party_dyadpid_lim="christdem_liberal" if party_dyadpid=="liberal_christdem"
	replace party_dyadpid_lim="christdem_socialist" if party_dyadpid=="socialist_christdem"
	replace party_dyadpid_lim="christdem_rad left" if party_dyadpid=="rad left_christdem"
	replace party_dyadpid_lim="cons_liberal" if party_dyadpid=="liberal_cons"
	replace party_dyadpid_lim="cons_socialist" if party_dyadpid=="socialist_cons"
	replace party_dyadpid_lim="cons_rad left" if party_dyadpid=="rad left_cons"
	replace party_dyadpid_lim="liberal_rad left" if party_dyadpid=="rad left_liberal"
	replace party_dyadpid_lim="liberal_socialist" if party_dyadpid=="socialist_liberal"
	replace party_dyadpid_lim="rad left_socialist" if party_dyadpid=="socialist_rad left"

	tab party_dyadpid_lim
	replace party_dyadpid_lim="" if family_pid==family		// drop within-family dyads
	encode party_dyadpid_lim, gen(party_dyadpid_lim_num)
	tab party_dyadpid_lim_num, nolabel

	eststo dyad2019: vce2way reg ptv b15.party_dyadpid_lim_num i.country if family_pid!=. & year==2019, cluster(respid country)
	margins party_dyadpid_lim_num , saving("dyadmargins2019pid", replace)

	preserve
	use "dyadmargins2019pid", clear
	sort _margin 
	gen obsno = _n 
	labmask obsno, values(_m1) lblname(_m1) decode
	twoway scatter obsno _margin, yla(1/21, valuelabel  noticks) msymbol(O) msize(medlarge)  || rspike _ci_lb _ci_ub obsno, horizontal xtitle("Predicted PTV") ytitle("") xlabel(0(1)6) legend(off) title("") saving(ptvbydyadpid_2019.gph, replace)
	restore
	
	
** Table G.1: Explaining PTV ratings with information on ideological distance between the party voted for and party that is evaluated, non-linear effects	
** Code below also produces output for Figures G.1. and G.2. 	

	* load and pool data
	use   "data/ees-votersurvey-1999-coded-r&r.dta", clear
	append using "data/ees-votersurvey-2009-coded-r&r.dta"
	append using "data/ees-votersurvey-2014-coded-r&r.dta"
	append using "data/ees-votersurvey-2019-coded-r&r.dta"
	replace respid=var001 if year==1999 		// 1999 data had a different respondent id variable name
	replace countryweight=1 if countryweight==.	// 1999 data have a country-weight, give weight of 1 to observations of all other years
	
	* create 'wave' variables for plotting estimates at equal distance on x-axis
	gen wave=1 if year==1999
	replace wave=2 if year==2009
	replace wave=3 if year==2014
	replace wave=4 if year==2019

	* estimation pooled data
	eststo nonlinear_pooled: vce2way reg ptv c.distance_lrecon##c.distance_lrecon c.distance_galtan##c.distance_galtan i.country i.year if votedforparty!=1 [aweight=countryweight], cluster(respid country)
	margins , at(distance_lrecon=(0(0.5)8.5))
	marginsplot, title("") recast(line) recastci(rarea) yline(0, lpattern(dash)) xtitle("Economic left-right distance") xlabel(0(2)9) ylabel(-1(1)5) plotopts(msymbol(O) msize(medlarge))  saving(ecnonlinearpooled.gph, replace)
	margins , at(distance_galtan=(0(0.5)8.5))
	marginsplot, title("") recast(line) recastci(rarea) yline(0, lpattern(dash)) xtitle("GAL/TAN distance") xlabel(0(2)9) ylabel(-1(1)5) plotopts(msymbol(O) msize(medlarge))  saving(galtannonlinearpooled.gph, replace)
	
	* estimation 1999
	eststo nonlinear_1999: vce2way reg ptv c.distance_lrecon##c.distance_lrecon c.distance_galtan##c.distance_galtan i.country if votedforparty!=1 & year==1999 [aweight=countryweight], cluster(respid country)
	margins , at(distance_lrecon=(0(0.5)8.5))
	marginsplot, title("") recast(line) recastci(rarea) yline(0, lpattern(dash)) xtitle("Economic left-right distance") xlabel(0(2)9) ylabel(-1(1)5) plotopts(msymbol(O) msize(medlarge))  saving(ecnonlinear1999.gph, replace)
	margins , at(distance_galtan=(0(0.5)8.5))
	marginsplot, title("") recast(line) recastci(rarea) yline(0, lpattern(dash)) xtitle("GAL/TAN distance") xlabel(0(2)9) ylabel(-1(1)5) plotopts(msymbol(O) msize(medlarge))  saving(galtannonlinear1999.gph, replace)	
	
	* estimation 2009
	eststo nonlinear_2009: vce2way reg ptv c.distance_lrecon##c.distance_lrecon c.distance_galtan##c.distance_galtan i.country if votedforparty!=1 & year==2009 [aweight=countryweight], cluster(respid country)
	margins , at(distance_lrecon=(0(0.5)8.5))
	marginsplot, title("") recast(line) recastci(rarea) yline(0, lpattern(dash)) xtitle("Economic left-right distance") xlabel(0(2)9) ylabel(-1(1)5) plotopts(msymbol(O) msize(medlarge))  saving(ecnonlinear2009.gph, replace)
	margins , at(distance_galtan=(0(0.5)8.5))
	marginsplot, title("") recast(line) recastci(rarea) yline(0, lpattern(dash)) xtitle("GAL/TAN distance") xlabel(0(2)9) ylabel(-1(1)5) plotopts(msymbol(O) msize(medlarge))  saving(galtannonlinear2009.gph, replace)		
	
	* estimation 2014
	eststo nonlinear_2014: vce2way reg ptv c.distance_lrecon##c.distance_lrecon c.distance_galtan##c.distance_galtan i.country if votedforparty!=1 & year==2014 [aweight=countryweight], cluster(respid country)
	margins , at(distance_lrecon=(0(0.5)8.5))
	marginsplot, title("") recast(line) recastci(rarea) yline(0, lpattern(dash)) xtitle("Economic left-right distance") xlabel(0(2)9) ylabel(-1(1)5) plotopts(msymbol(O) msize(medlarge))  saving(ecnonlinear2014.gph, replace)
	margins , at(distance_galtan=(0(0.5)8.5))
	marginsplot, title("") recast(line) recastci(rarea) yline(0, lpattern(dash)) xtitle("GAL/TAN distance") xlabel(0(2)9) ylabel(-1(1)5) plotopts(msymbol(O) msize(medlarge))  saving(galtannonlinear2014.gph, replace)		
	
	* estimation 2019 
	eststo nonlinear_2019: vce2way reg ptv c.distance_lrecon##c.distance_lrecon c.distance_galtan##c.distance_galtan i.country if votedforparty!=1 & year==2019 [aweight=countryweight], cluster(respid country)	
	margins , at(distance_lrecon=(0(0.5)8.5))
	marginsplot, title("") recast(line) recastci(rarea) yline(0, lpattern(dash)) xtitle("Economic left-right distance") xlabel(0(2)9) ylabel(-1(1)5) saving(ecnonlinear2019.gph, replace)
	margins , at(distance_galtan=(0(0.5)8.5))
	marginsplot, title("") recast(line) recastci(rarea) yline(0, lpattern(dash)) xtitle("GAL/TAN distance") xlabel(0(2)9) ylabel(-1(1)5) plotopts(msymbol(O) msize(medlarge))  saving(galtannonlinear2019.gph, replace)		
	
	* table output (Table G.1)
	esttab nonlinear_pooled nonlinear_1999 nonlinear_2009 nonlinear_2014 nonlinear_2019 using nonlinear.tex, cells(b(star fmt(3)) se(par fmt(3)))  collab(none) stats(N r2 , fmt(0 3)) legend numbers  drop (*.country *.year) nogap replace
	
	
** Figure J.1: Predicted PTV by party family dyad – 1999 EES

	** pool the 1999, 2009, 2014 and 2019 data
	use   "data/ees-votersurvey-1999-coded-r&r.dta", clear
	append using "data/ees-votersurvey-2009-coded-r&r.dta"
	append using "data/ees-votersurvey-2014-coded-r&r.dta"
	append using "data/ees-votersurvey-2019-coded-r&r.dta"
	
	replace respid=var001 if year==1999 		// 1999 data had a different respondent id variable name
	replace countryweight=1 if countryweight==.	// 1999 data have a country-weight, give weight of 1 to observations of all other years

	* recode the party dyad variable 
	decode family_voted, gen(family_voted_string)
	decode family, gen(family_string)
	gen party_dyad=family_voted_string+"_"+family_string
	replace party_dyad="" if family_voted==.
	replace party_dyad="" if family==.
	drop if party_dyad==""
	replace party_dyad="" if family_voted==family		// drop within-family dyads
	encode party_dyad, gen(party_dyad_num)

	** arrange such that rad right is the first leg || next, green first leg
	gen party_dyad_lim=party_dyad	
	replace party_dyad_lim="tan_cons" if party_dyad=="cons_tan" 
	replace party_dyad_lim="tan_liberal" if party_dyad=="liberal_tan"
	replace party_dyad_lim="tan_christdem" if party_dyad=="christdem_tan" 
	replace party_dyad_lim="tan_socialist" if party_dyad=="socialist_tan"
	replace party_dyad_lim="tan_rad left" if party_dyad=="rad left_tan"
	replace party_dyad_lim="tan_green" if party_dyad=="green_tan"
	replace party_dyad_lim="green_cons" if party_dyad=="cons_green"
	replace party_dyad_lim="green_liberal" if party_dyad=="liberal_green"
	replace party_dyad_lim="green_christdem" if party_dyad=="christdem_green"
	replace party_dyad_lim="green_socialist" if party_dyad=="socialist_green"
	replace party_dyad_lim="green_rad left" if party_dyad=="rad left_green"
	replace party_dyad_lim="christdem_cons" if party_dyad=="cons_christdem"
	replace party_dyad_lim="christdem_liberal" if party_dyad=="liberal_christdem"
	replace party_dyad_lim="christdem_socialist" if party_dyad=="socialist_christdem"
	replace party_dyad_lim="christdem_rad left" if party_dyad=="rad left_christdem"
	replace party_dyad_lim="cons_liberal" if party_dyad=="liberal_cons"
	replace party_dyad_lim="cons_socialist" if party_dyad=="socialist_cons"
	replace party_dyad_lim="cons_rad left" if party_dyad=="rad left_cons"
	replace party_dyad_lim="liberal_rad left" if party_dyad=="rad left_liberal"
	replace party_dyad_lim="liberal_socialist" if party_dyad=="socialist_liberal"
	replace party_dyad_lim="rad left_socialist" if party_dyad=="socialist_rad left"

	tab party_dyad_lim
	replace party_dyad_lim="" if family_voted==family		// drop within-family dyads
	encode party_dyad_lim, gen(party_dyad_lim_num)
	tab party_dyad_lim_num, nolabel

	eststo dyad1999: vce2way reg ptv b15.party_dyad_lim_num i.country if family_voted!=. & year==1999 [aweight=countryweight], cluster(respid country)
	margins party_dyad_lim_num , saving("dyadmargins1999", replace)
	
	preserve
	use "dyadmargins1999", clear
	sort _margin 
	gen obsno = _n 
	labmask obsno, values(_m1) lblname(_m1) decode
	twoway scatter obsno _margin, yla(1/21, valuelabel  noticks) msymbol(O) msize(medlarge)  || rspike _ci_lb _ci_ub obsno, horizontal xtitle("Predicted PTV") ytitle("") xlabel(0(1)6) legend(off) title("") saving(ptvbydyad_1999.gph, replace)
	restore
	
** Figure J.2: Predicted PTV by party family dyad – 2009 EES	
	
	eststo dyad2009: vce2way reg ptv b15.party_dyad_lim_num i.country if family_voted!=. & year==2009, cluster(respid country)
	margins party_dyad_lim_num , saving("dyadmargins2009", replace)
	
	preserve
	use "dyadmargins2009", clear
	sort _margin 
	gen obsno = _n 
	labmask obsno, values(_m1) lblname(_m1) decode
	twoway scatter obsno _margin, yla(1/21, valuelabel  noticks) msymbol(O) msize(medlarge)  || rspike _ci_lb _ci_ub obsno, horizontal xtitle("Predicted PTV") ytitle("") xlabel(0(1)6) legend(off) title("") saving(ptvbydyad_2009.gph, replace)
	restore

** Figure J.3: Predicted PTV by party family dyad – 2014 EES	
	
	eststo dyad2014: vce2way reg ptv b15.party_dyad_lim_num i.country if family_voted!=. & year==2014, cluster(respid country)
	margins party_dyad_lim_num , saving("dyadmargins2014", replace)
	
	preserve
	use "dyadmargins2014", clear
	sort _margin 
	gen obsno = _n 
	labmask obsno, values(_m1) lblname(_m1) decode
	twoway scatter obsno _margin, yla(1/21, valuelabel  noticks) msymbol(O) msize(medlarge)  || rspike _ci_lb _ci_ub obsno, horizontal xtitle("Predicted PTV") ytitle("") xlabel(0(1)6) legend(off) title("") saving(ptvbydyad_2014.gph, replace)
	restore

	
** Figure K.1: Predicted PTV by party family dyad, disaggregating electorates and party evaluated

	eststo dyad2019: vce2way reg ptv b15.party_dyad_num i.country if family_voted!=. & year==2019, cluster(respid country)
	margins party_dyad_num , saving("dyadmargins2019full", replace)
	
	preserve
	use "dyadmargins2019full", clear
	sort _margin 
	gen obsno = _n 
	labmask obsno, values(_m1) lblname(_m1) decode
	twoway scatter obsno _margin, yla(1/42, valuelabel  noticks) msymbol(O) msize(medlarge)  || rspike _ci_lb _ci_ub obsno, horizontal xtitle("Predicted PTV") ytitle("") xlabel(0(1)6) legend(off) title("") saving(ptvbydyad_2019full.gph, replace)
	restore
		
	
** Figure L.1: Predicted vote propensity (PTV) for party families and their voters – 1999 EES	
	
	* panel a
	vce2way reg ptv i.family i.country if votedforparty!=1 & family_voted!=.  & year==1999 [aweight=countryweight], cluster(respid country)
	margins family
	marginsplot, xtitle("Party family that is evaluated") ytitle("Predicted PTV") title("") recast(scatter) plot1opts(msymbol(O)) recastci(rspike) yline(2.30, lpattern(dash)) xlabel(, labsize(small) ) ylabel(0(1)5) title("") plotopts(msymbol(O) msize(medlarge))  saving(tanobject_1999.gph, replace)

	* panel b
	vce2way reg ptv i.family_voted i.country  if votedforparty!=1 & year==1999 [aweight=countryweight], cluster(respid country)
	margins family_voted
	marginsplot, xtitle("Voted for party of party family...") ytitle("Predicted PTV other parties") title("") recast(scatter) plot1opts(msymbol(O)) recastci(rspike) yline(2.30, lpattern(dash)) xlabel(, labsize(small) ) ylabel(0(1)5) title("") plotopts(msymbol(O) msize(medlarge))  saving(tansubject_1999.gph, replace)


** Figure L.2: Predicted vote propensity (PTV) for party families and their voters – 2009 EES
	
	* panel a
	vce2way reg ptv i.family i.country  if votedforparty!=1 & family_voted!=.  & year==2009 , cluster(respid country)
	margins family
	marginsplot, xtitle("Party family that is evaluated") ytitle("Predicted PTV") title("") recast(scatter) plot1opts(msymbol(O)) recastci(rspike) yline(2.72, lpattern(dash)) xlabel(, labsize(small) ) ylabel(0(1)5) title("") plotopts(msymbol(O) msize(medlarge))  saving(tanobject_2009.gph, replace)
	
	* panel b
	vce2way reg ptv i.family_voted i.country  if votedforparty!=1 & year==2009 , cluster(respid country)
	margins family_voted
	marginsplot, xtitle("Voted for party of party family...") ytitle("Predicted PTV other parties") title("") recast(scatter) plot1opts(msymbol(O)) recastci(rspike) yline(2.72, lpattern(dash)) xlabel(, labsize(small) ) ylabel(0(1)5) title("") plotopts(msymbol(O) msize(medlarge))  saving(tansubject_2009.gph, replace)		
	

** Figure L.3: Predicted vote propensity (PTV) for party families and their voters – 2014 EES	
	
	* panel a
	vce2way reg ptv i.family i.country  if votedforparty!=1 & family_voted!=.  & year==2014, cluster(respid country)
	margins family
	marginsplot, xtitle("Party family that is evaluated") ytitle("Predicted PTV") title("") recast(scatter) plot1opts(msymbol(O)) recastci(rspike) yline(2.58, lpattern(dash)) xlabel(, labsize(small) ) ylabel(0(1)5)  title("") plotopts(msymbol(O) msize(medlarge))  saving(tanobject_2014.gph, replace)

	* panel b
	vce2way reg ptv i.family_voted i.country  if votedforparty!=1 & year==2014 , cluster(respid country)
	margins family_voted
	marginsplot, xtitle("Voted for party of party family...") ytitle("Predicted PTV other parties") title("") recast(scatter) plot1opts(msymbol(O)) recastci(rspike) yline(2.58, lpattern(dash)) xlabel(, labsize(small) ) ylabel(0(1)5) title("") plotopts(msymbol(O) msize(medlarge))  saving(tansubject_2014.gph, replace)		

	
** Figure M.1: Change in the coefficients of party family dyads when accounting for economic left-right and GAL-TAN distance, 2009	

	preserve
	use "data/coefreduced-2009.dta", clear
	
	twoway dropline amediff1 partydyadnum   , horizontal ylabel(1(1)20, valuelabel) xsize(4) ysize(6) 	xtitle("Reduction in party dyad coefficient") ytitle("") xlabel(-0.5(0.5)2) saving(coef1-2009.gph, replace)
	twoway dropline amediff2 partydyadnum   , horizontal ylabel(1(1)20, valuelabel) xsize(4) ysize(6) xtitle("Reduction in party dyad coefficient") ytitle("") xlabel(-0.5(0.5)2) saving(coef2-2009.gph, replace)
	twoway dropline amediff3 partydyadnum   , horizontal ylabel(1(1)20, valuelabel) xsize(4) ysize(6) xtitle("Reduction in party dyad coefficient") ytitle("") xlabel(-0.5(0.5)2) saving(coef3-2009.gph, replace)
	restore
	
** Figure M.2: Change in the coefficients of party family dyads when accounting for economic left-right and GAL-TAN distance, 2014

	preserve
	use "data/coefreduced-2014.dta", clear
	
	twoway dropline amediff1 partydyadnum   , horizontal ylabel(1(1)20, valuelabel) xsize(4) ysize(6) 	xtitle("Reduction in party dyad coefficient") xlabel(-0.5(0.5)2) ytitle("") saving(coef1-2014.gph, replace)
	twoway dropline amediff2 partydyadnum   , xlabel(-0.5(0.5)2) horizontal ylabel(1(1)20, valuelabel) xsize(4) ysize(6) xtitle("Reduction in party dyad coefficient") ytitle("") saving(coef2-2014.gph, replace)
	twoway dropline amediff3 partydyadnum   , xlabel(-0.5(0.5)2) horizontal ylabel(1(1)20, valuelabel) xsize(4) ysize(6) xtitle("Reduction in party dyad coefficient") ytitle("") saving(coef3-2014.gph, replace)
	restore 
	
** Table N.1: Change in coefficients of party family dyads, detailed estimates – 1999	

	eststo Dis1: vce2way reg ptv b12.party_dyad_lim_num i.country if family_voted!=. & year==1999, cluster(respid country)
	eststo Dis2: vce2way reg ptv b12.party_dyad_lim_num distance_lrecon i.country if family_voted!=. & year==1999, cluster(respid country)
	eststo Dis3: vce2way reg ptv b12.party_dyad_lim_num distance_galtan i.country if family_voted!=. & year==1999, cluster(respid country)
	eststo Dis4: vce2way reg ptv b12.party_dyad_lim_num distance_lrecon distance_galtan i.country if family_voted!=. & year==1999, cluster(respid country)
	
	esttab Dis1 Dis2 Dis3 Dis4 using appendix_reducedcoef1999.tex, cells(b(star fmt(3)) se(par fmt(3)))  collab(none) stats(N r2 aic bic, fmt(0 3)) legend numbers  drop (*.country ) nogap replace
	
** Table N.2: Change in coefficients of party family dyads, detailed estimates – 2009	

	eststo Dis1: vce2way reg ptv b12.party_dyad_lim_num i.country if family_voted!=. & year==2009, cluster(respid country)
	eststo Dis2: vce2way reg ptv b12.party_dyad_lim_num distance_lrecon i.country if family_voted!=. & year==2009, cluster(respid country)
	eststo Dis3: vce2way reg ptv b12.party_dyad_lim_num distance_galtan i.country if family_voted!=. & year==2009, cluster(respid country)
	eststo Dis4: vce2way reg ptv b12.party_dyad_lim_num distance_lrecon distance_galtan i.country if family_voted!=. & year==2009, cluster(respid country)
	
	esttab Dis1 Dis2 Dis3 Dis4 using appendix_reducedcoef2009.tex, cells(b(star fmt(3)) se(par fmt(3)))  collab(none) stats(N r2 aic bic, fmt(0 3)) legend numbers  drop (*.country ) nogap replace
	
** Table N.3: Change in coefficients of party family dyads, detailed estimates – 2014	

	eststo Dis1: vce2way reg ptv b12.party_dyad_lim_num i.country if family_voted!=. & year==2014, cluster(respid country)
	eststo Dis2: vce2way reg ptv b12.party_dyad_lim_num distance_lrecon i.country if family_voted!=. & year==2014, cluster(respid country)
	eststo Dis3: vce2way reg ptv b12.party_dyad_lim_num distance_galtan i.country if family_voted!=. & year==2014, cluster(respid country)
	eststo Dis4: vce2way reg ptv b12.party_dyad_lim_num distance_lrecon distance_galtan i.country if family_voted!=. & year==2014, cluster(respid country)
	
	esttab Dis1 Dis2 Dis3 Dis4 using appendix_reducedcoef2014.tex, cells(b(star fmt(3)) se(par fmt(3)))  collab(none) stats(N r2 aic bic, fmt(0 3)) legend numbers  drop (*.country ) nogap replace
	
** Table N.4: Change in coefficients of party family dyads, detailed estimates – 2019

	eststo Dis1: vce2way reg ptv b12.party_dyad_lim_num i.country if family_voted!=., cluster(id_num country)
	eststo Dis2: vce2way reg ptv b12.party_dyad_lim_num distance_lrecon i.country if family_voted!=., cluster(id_num country)
	eststo Dis3: vce2way reg ptv b12.party_dyad_lim_num distance_galtan i.country if family_voted!=., cluster(id_num country)
	eststo Dis4: vce2way reg ptv b12.party_dyad_lim_num distance_lrecon distance_galtan i.country if family_voted!=., cluster(id_num country)
	
	esttab Dis1 Dis2 Dis3 Dis4 using appendix_reducedcoef2019.tex, cells(b(star fmt(3)) se(par fmt(3)))  collab(none) stats(N r2 aic bic, fmt(0 3)) legend numbers  drop (*.country ) nogap replace
	
	
** Figure O.1: Average marginal effect of distance on PTVs, change over time – excluding Green and radical right parties

	* create 'wave' variables for plotting estimates at equal distance on x-axis
	gen wave=1 if year==1999
	replace wave=2 if year==2009
	replace wave=3 if year==2014
	replace wave=4 if year==2019

	* estimation 
	eststo change1: vce2way reg ptv i.wave##c.distance_lrecon i.wave##c.distance_galtan i.country if votedforparty!=1 & family!=1 & family!=7 [aweight=countryweight], cluster(respid country)	
	
	* panel a
	margins , dydx(distance_lrecon) at(wave=(1 2 3 4 ))
	marginsplot , title("") recast(scatter) recastci(rspike) yline(0, lpattern(dash)) xtitle("") xlabel(1 "1999" 2 "2009" 3 "2014" 4 "2019") ylabel(-0.6(0.2)0.2) title("") plotopts(msymbol(O) msize(medlarge))  saving(ecotrendmainstream.gph, replace)
	
	* panel b
	margins , dydx(distance_galtan) at(wave=(1 2 3 4))
	marginsplot, title("") recast(scatter) recastci(rspike) yline(0, lpattern(dash)) xtitle("") xlabel(1 "1999" 2 "2009" 3 "2014" 4 "2019") ylabel(-0.6(0.2)0.2) title("") plotopts(msymbol(O) msize(medlarge))  saving(galtantrendmainstream.gph, replace)
	
** Figure Q.1: Average marginal effect of GAL/TAN distance on PTVs over time, excluding Austria
	eststo changeNOAT: vce2way reg ptv i.wave##c.distance_lrecon i.wave##c.distance_galtan i.country if votedforparty!=1 & country!=13 [aweight=countryweight], cluster(respid country)	
	
	margins , dydx(distance_galtan) at(wave=(1 2 3 4))
	marginsplot, title("") recast(scatter) recastci(rspike) yline(0, lpattern(dash)) xtitle("") xlabel(1 "1999" 2 "2009" 3 "2014" 4 "2019") ylabel(-0.4(0.2)0.2) title("") plotopts(msymbol(O) msize(medlarge))  saving(galtantrendNOAT.gph, replace)	

	
** Figure Q.2: Average marginal effect of GAL/TAN distance on PTVs over time, excluding Belgium
	eststo changeNOBE: vce2way reg ptv i.wave##c.distance_lrecon i.wave##c.distance_galtan i.country if votedforparty!=1 & country!=1 [aweight=countryweight], cluster(respid country)	
	
	margins , dydx(distance_galtan) at(wave=(1 2 3 4))
	marginsplot, title("") recast(scatter) recastci(rspike) yline(0, lpattern(dash)) xtitle("") xlabel(1 "1999" 2 "2009" 3 "2014" 4 "2019") ylabel(-0.4(0.2)0.2) title("") plotopts(msymbol(O) msize(medlarge))  saving(galtantrendNOBE.gph, replace)
	
** Figure Q.3: Average marginal effect of GAL/TAN distance on PTVs over time, excluding Denmark
	eststo changeNODK: vce2way reg ptv i.wave##c.distance_lrecon i.wave##c.distance_galtan i.country if votedforparty!=1 & country!=2 [aweight=countryweight], cluster(respid country)	
	
	margins , dydx(distance_galtan) at(wave=(1 2 3 4))
	marginsplot, title("") recast(scatter) recastci(rspike) yline(0, lpattern(dash)) xtitle("") xlabel(1 "1999" 2 "2009" 3 "2014" 4 "2019") ylabel(-0.4(0.2)0.2) title("") plotopts(msymbol(O) msize(medlarge))  saving(galtantrendNODK.gph, replace)
	

** Figure Q.4: Average marginal effect of GAL/TAN distance on PTVs over time, excluding Finland	
	eststo changeNOFI: vce2way reg ptv i.wave##c.distance_lrecon i.wave##c.distance_galtan i.country if votedforparty!=1 & country!=14 [aweight=countryweight], cluster(respid country)	
	
	margins , dydx(distance_galtan) at(wave=(1 2 3 4))
	marginsplot, title("") recast(scatter) recastci(rspike) yline(0, lpattern(dash)) xtitle("") xlabel(1 "1999" 2 "2009" 3 "2014" 4 "2019") ylabel(-0.4(0.2)0.2) title("") plotopts(msymbol(O) msize(medlarge))  saving(galtantrendNOFI.gph, replace)		
	
** Figure Q.5: Average marginal effect of GAL/TAN distance on PTVs over time, excluding France	
	eststo changeNOFR: vce2way reg ptv i.wave##c.distance_lrecon i.wave##c.distance_galtan i.country if votedforparty!=1 & country!=6 [aweight=countryweight], cluster(respid country)	
	
	margins , dydx(distance_galtan) at(wave=(1 2 3 4))
	marginsplot, title("") recast(scatter) recastci(rspike) yline(0, lpattern(dash)) xtitle("") xlabel(1 "1999" 2 "2009" 3 "2014" 4 "2019") ylabel(-0.4(0.2)0.2) title("") plotopts(msymbol(O) msize(medlarge))  saving(galtantrendNOFR.gph, replace)	
	
** Figure Q.6: Average marginal effect of GAL/TAN distance on PTVs over time, excluding Germany
	eststo changeNODE: vce2way reg ptv i.wave##c.distance_lrecon i.wave##c.distance_galtan i.country if votedforparty!=1 & country!=3 [aweight=countryweight], cluster(respid country)	
	
	margins , dydx(distance_galtan) at(wave=(1 2 3 4))
	marginsplot, title("") recast(scatter) recastci(rspike) yline(0, lpattern(dash)) xtitle("") xlabel(1 "1999" 2 "2009" 3 "2014" 4 "2019") ylabel(-0.4(0.2)0.2) title("") plotopts(msymbol(O) msize(medlarge))  saving(galtantrendNODE.gph, replace)
	
** Figure Q.7: Average marginal effect of GAL/TAN distance on PTVs over time, excluding Greece
	eststo changeNOGR: vce2way reg ptv i.wave##c.distance_lrecon i.wave##c.distance_galtan i.country if votedforparty!=1 & country!=4 [aweight=countryweight], cluster(respid country)	
	
	margins , dydx(distance_galtan) at(wave=(1 2 3 4))
	marginsplot, title("") recast(scatter) recastci(rspike) yline(0, lpattern(dash)) xtitle("") xlabel(1 "1999" 2 "2009" 3 "2014" 4 "2019") ylabel(-0.4(0.2)0.2) title("") plotopts(msymbol(O) msize(medlarge))  saving(galtantrendNOGR.gph, replace)	
	
** Figure Q.8: Average marginal effect of GAL/TAN distance on PTVs over time, excluding Ireland	
	eststo changeNOIE: vce2way reg ptv i.wave##c.distance_lrecon i.wave##c.distance_galtan i.country if votedforparty!=1 & country!=7 [aweight=countryweight], cluster(respid country)	
	
	margins , dydx(distance_galtan) at(wave=(1 2 3 4))
	marginsplot, title("") recast(scatter) recastci(rspike) yline(0, lpattern(dash)) xtitle("") xlabel(1 "1999" 2 "2009" 3 "2014" 4 "2019") ylabel(-0.4(0.2)0.2) title("") plotopts(msymbol(O) msize(medlarge))  saving(galtantrendNOIE.gph, replace)	
	
** Figure Q.9: Average marginal effect of GAL/TAN distance on PTVs over time, excluding Italy	
	eststo changeNOIT: vce2way reg ptv i.wave##c.distance_lrecon i.wave##c.distance_galtan i.country if votedforparty!=1 & country!=8 [aweight=countryweight], cluster(respid country)	
	
	margins , dydx(distance_galtan) at(wave=(1 2 3 4))
	marginsplot, title("") recast(scatter) recastci(rspike) yline(0, lpattern(dash)) xtitle("") xlabel(1 "1999" 2 "2009" 3 "2014" 4 "2019") ylabel(-0.4(0.2)0.2) title("") plotopts(msymbol(O) msize(medlarge))  saving(galtantrendNOIT.gph, replace)	
	
** Figure Q.10: Average marginal effect of GAL/TAN distance on PTVs over time, excluding Netherlands	
	eststo changeNONL: vce2way reg ptv i.wave##c.distance_lrecon i.wave##c.distance_galtan i.country if votedforparty!=1 & country!=10 [aweight=countryweight], cluster(respid country)	
	
	margins , dydx(distance_galtan) at(wave=(1 2 3 4))
	marginsplot, title("") recast(scatter) recastci(rspike) yline(0, lpattern(dash)) xtitle("") xlabel(1 "1999" 2 "2009" 3 "2014" 4 "2019") ylabel(-0.4(0.2)0.2) title("") plotopts(msymbol(O) msize(medlarge))  saving(galtantrendNONL.gph, replace)		
	
** Figure Q.11: Average marginal effect of GAL/TAN distance on PTVs over time, excluding Portugal	
	eststo changeNOPT: vce2way reg ptv i.wave##c.distance_lrecon i.wave##c.distance_galtan i.country if votedforparty!=1 & country!=12 [aweight=countryweight], cluster(respid country)	
	
	margins , dydx(distance_galtan) at(wave=(1 2 3 4))
	marginsplot, title("") recast(scatter) recastci(rspike) yline(0, lpattern(dash)) xtitle("") xlabel(1 "1999" 2 "2009" 3 "2014" 4 "2019") ylabel(-0.4(0.2)0.2) title("") plotopts(msymbol(O) msize(medlarge))  saving(galtantrendNOPT.gph, replace)		

** Figure Q.12: Average marginal effect of GAL/TAN distance on PTVs over time, excluding Spain
	eststo changeNOES: vce2way reg ptv i.wave##c.distance_lrecon i.wave##c.distance_galtan i.country if votedforparty!=1 & country!=5 [aweight=countryweight], cluster(respid country)	
	
	margins , dydx(distance_galtan) at(wave=(1 2 3 4))
	marginsplot, title("") recast(scatter) recastci(rspike) yline(0, lpattern(dash)) xtitle("") xlabel(1 "1999" 2 "2009" 3 "2014" 4 "2019") ylabel(-0.4(0.2)0.2) title("") plotopts(msymbol(O) msize(medlarge))  saving(galtantrendNOES.gph, replace)	
	
** Figure Q.13: Average marginal effect of GAL/TAN distance on PTVs over time, excluding Sweden
	eststo changeNOSE: vce2way reg ptv i.wave##c.distance_lrecon i.wave##c.distance_galtan i.country if votedforparty!=1 & country!=16 [aweight=countryweight], cluster(respid country)	
	
	margins , dydx(distance_galtan) at(wave=(1 2 3 4))
	marginsplot, title("") recast(scatter) recastci(rspike) yline(0, lpattern(dash)) xtitle("") xlabel(1 "1999" 2 "2009" 3 "2014" 4 "2019") ylabel(-0.4(0.2)0.2) title("") plotopts(msymbol(O) msize(medlarge))  saving(galtantrendNOSE.gph, replace)		
	
** Figure Q.14: Average marginal effect of GAL/TAN distance on PTVs over time, excluding United Kingdom
	eststo changeNOUK: vce2way reg ptv i.wave##c.distance_lrecon i.wave##c.distance_galtan i.country if votedforparty!=1 & country!=11 [aweight=countryweight], cluster(respid country)	
	
	margins , dydx(distance_galtan) at(wave=(1 2 3 4))
	marginsplot, title("") recast(scatter) recastci(rspike) yline(0, lpattern(dash)) xtitle("") xlabel(1 "1999" 2 "2009" 3 "2014" 4 "2019") ylabel(-0.4(0.2)0.2) title("") plotopts(msymbol(O) msize(medlarge))  saving(galtantrendNOUK.gph, replace)	
	

** Table R.1: Explaining PTV with ideological distance between party voted and party evaluated, pooled data – Focus on EU integration

	* estimation
	eststo distance1: vce2way reg ptv  distance_eu i.country i.year if votedforparty!=1 [aweight=countryweight], cluster(respid country)
	eststo distance2: vce2way reg ptv distance_lrecon distance_galtan distance_eu i.country i.year if votedforparty!=1 [aweight=countryweight], cluster(respid country)
	
	* output table
	esttab distance1 distance2 using eudistance.rtf, cells(b(star fmt(3)) se(par fmt(3)))  collab(none) stats(N r2 , fmt(0 3)) legend numbers  drop (*.country *.year) nogap replace
	
** Figure R.1: Average marginal effect of European integration distance on PTVs, change over time
	eststo change1: vce2way reg ptv i.wave##c.distance_lrecon i.wave##c.distance_galtan i.wave##c.distance_eu  i.country if votedforparty!=1 [aweight=countryweight], cluster(respid country)	
	
	margins , dydx(distance_eu) at(wave=(1 2 3 4 ))
	marginsplot , title("") recast(scatter) recastci(rspike) yline(0, lpattern(dash)) xtitle("") xlabel(1 "1999" 2 "2009" 3 "2014" 4 "2019") ylabel(-0.4(0.2)0.2) title("") plotopts(msymbol(O) msize(medlarge))  saving(eutrend.gph, replace)
	
	
* ---------------------------------------------------------------	
* Appendix P replication (Using Danish election survey data)		
* ---------------------------------------------------------------

** prepare CMP data first

	use "data/MPDataset_MPDS2021a_stata14.dta", clear
	gen year=substr(string(date),1,4) 
	destring year, replace
	gen votedparty=party
	save "data/MPDataset_MPDS2021a_stata14-coded.dta", replace

** use Danish voter survey data

	use "data/kontinuitetsfil_1971_2019_27april2021.dta", clear

** data coding 
	
	* party like/dislike scales clean values
	recode v117 v118 v119 v120 v121 v122 v123 v124 v125 v126 v127 v128 v129 v130 (11=.) (12=.) (97=.) (98=.) (99=.) (999=.)
	sum v117 v118 v119 v120 v121 v122 v123 v124 v125 v126 v127 v128 v129 v130

	* rename party like/dislike variable 
	rename v117 like_party1		// soc.dem
	rename v118 like_party2		// det radikale venstre
	rename v119 like_party3		// konservative
	rename v120 like_party4		// cd
	rename v121 like_party5		// retsforbundet
	rename v122 like_party6		// socialistisk folkeparti
	rename v123 like_party7		// kommunisterne
	rename v124 like_party8		// kristendemokraterne 
	rename v125 like_party9		// dansk folkeparti
	rename v126 like_party10	// venstre
	rename v127 like_party11	// liberal alliance
	rename v128 like_party12	// venstresoc
	rename v129 like_party13	// fremskridtspartiet
	rename v130 like_party14	// enhedslisten

	* reshape data to long format
	gen id=_n
	reshape long like_party, i(id) j(partynumber)

	* add party key variable for merge with CMP data
	gen party=13320 if partynumber==1
	replace party=13410 if partynumber==2
	replace party=13620 if partynumber==3
	replace party=13330 if partynumber==4
	replace party=13952 if partynumber==5
	replace party=13230 if partynumber==6
	replace party=13220 if partynumber==7
	replace party=13520 if partynumber==8
	replace party=13720 if partynumber==9
	replace party=13420 if partynumber==10
	replace party=13001 if partynumber==11
	replace party=13210 if partynumber==12
	replace party=13951 if partynumber==13
	replace party=13229 if partynumber==14

	rename v1 year

	* merge in CMP info for party that is evaluated
	merge m:m party year using "~/dropbox/cross-pressured electorates and parties/data/cmp/MPDataset_MPDS2021a_stata14-coded.dta"
	drop if _merge==2
	drop _merge votedparty

	* create economic left-right and gal/tan positions of parties evaluated, using the Bakker/Hobolt proposal for combining CMP codes to generate economic left-right and gal/tan dimensions

	gen evaluated_ecrile=(per401+per402+per407+per505+per507+per410+per414+per702)-(per403+per404+per406+per504+per506+per413+per412+per701+per405+per409+per415+per503)
	gen evaluated_galtan=(per305+per601+per603+per605+per608+per606)-(per501+per602+per604+per502+per607+per416+per705+per706+per201+per202)

	rename partyname partyname_evaluated 
	rename partyabbrev partyabbrev_evaluated
	rename parfam parfam_evaluated
	drop per*
	drop country countryname oecdmember eumember edate date coderid manual coderyear testresult testeditsim  voteest presvote absseat totseats progtype datasetorigin corpusversion total planeco markeco welfare intpeace datasetversion manual coderid  oecd eu progtype voteest 

	rename party evaluatedparty

	* merge in CMP info for party respondent voted for
	gen vote=1 if v83==2			// soc.dem
	replace vote=2 if v83==3		// det radikale venstre
	replace vote=3 if v83==4		// konservative
	replace vote=4 if v83==5		// cd
	replace vote=5 if v83==6		// retsforbundet
	replace vote=6 if v83==7		// socialistisk folkeparti
	replace vote=7 if v83==12		// kommunisterne
	replace vote=8 if v83==13		// kristendemokraterne 
	replace vote=9 if v83==16		// dansk folkeparti
	replace vote=10 if v83==22	// venstre
	replace vote=11 if v83==10	// liberal alliance
	replace vote=12 if v83==23	// venstresoc
	replace vote=13 if v83==24	// fremskridtspartiet
	replace vote=14 if v83==25	// enhedslisten

	gen votedparty=13320 if vote==1
	replace votedparty=13410 if vote==2
	replace votedparty=13620 if vote==3
	replace votedparty=13330 if vote==4
	replace votedparty=13952 if vote==5
	replace votedparty=13230 if vote==6
	replace votedparty=13220 if vote==7
	replace votedparty=13520 if vote==8
	replace votedparty=13720 if vote==9
	replace votedparty=13420 if vote==10
	replace votedparty=13001 if vote==11
	replace votedparty=13210 if vote==12
	replace votedparty=13951 if vote==13
	replace votedparty=13229 if vote==14

	* merge in CMP info for party that one voted for
	merge m:m votedparty year using "data/MPDataset_MPDS2021a_stata14-coded.dta"
	drop if _merge==2
	drop _merge

	* create economic left-right and gal/tan positions of parties evaluated, using the Bakker/Hobolt proposal for combining CMP codes to generate economic left-right and gal/tan dimensions

	gen voted_ecrile=(per401+per402+per407+per505+per507+per410+per414+per702)-(per403+per404+per406+per504+per506+per413+per412+per701+per405+per409+per415+per503)
	gen voted_galtan=(per305+per601+per603+per605+per608+per606)-(per501+per602+per604+per502+per607+per416+per705+per706+per201+per202)

	rename partyname partyname_voted 
	rename partyabbrev partyabbrev_voted
	rename parfam parfam_voted
	drop per*
	drop country countryname oecdmember eumember edate date coderid manual coderyear testresult testeditsim  voteest presvote absseat totseats progtype datasetorigin corpusversion total planeco markeco welfare intpeace datasetversion manual coderid  oecd eu progtype voteest 

	** create distance measures, and rescale positions to 0-10 scales
	replace evaluated_ecrile=(evaluated_ecrile+100)/20
	replace evaluated_galtan=(evaluated_galtan+100)/20
	replace voted_ecrile=(voted_ecrile+100)/20
	replace voted_galtan=(voted_galtan+100)/20

	gen ecrile_distance=abs(evaluated_ecrile-voted_ecrile)
	gen galtan_distance=abs(evaluated_galtan-voted_galtan)

	gen time=year-1971		// set time variable so 1971==0

	** create party family dyads based on CMP families
	decode parfam_evaluated, gen(parfam_evaluated_string)
	decode parfam_voted, gen(parfam_voted_string)
	gen party_dyadcmp=parfam_voted_string+"_"+parfam_evaluated_string
	replace party_dyadcmp="" if parfam_evaluated==.
	replace party_dyadcmp="" if parfam_voted==.
	drop if party_dyadcmp==""
	replace party_dyadcmp="" if parfam_evaluated_string==parfam_voted_string		// code within-family dyads as missing so they are not included in the analyses
	encode party_dyadcmp, gen(party_dyadcmp_num)

	** code CHES based party families

	gen chesfamilyvoted="socialist" if vote==1 				// soc.dem / 1
	replace chesfamilyvoted="liberal" if vote==2			// det radikale venstre /2 
	replace chesfamilyvoted="cons" if vote==3				// konservative /3
	replace chesfamilyvoted="liberal" if vote==4			// cd /4
	replace chesfamilyvoted="" if vote==5					// retsforbundet /5		--> not coded by CHES
	replace chesfamilyvoted="rad left" if vote==6			// socialistisk folkeparti /6 note that CHES codes the party as green in 2019
	replace chesfamilyvoted="rad left" if vote==7			// kommunisterne /7 --> not coded by CHES but seems safe to code as radical left
	replace chesfamilyvoted="christdem" if vote==8			// kristendemokraterne /8 --> CHES codes as confessionalist, but we merged that category with Christian-democrats for EES analyses
	replace chesfamilyvoted="tan" if vote==9			// dansk folkeparti /9
	replace chesfamilyvoted="liberal" if vote==10			// venstre /10
	replace chesfamilyvoted="liberal" if vote==11			// liberal alliance /11
	replace chesfamilyvoted="rad left" if vote==12			// venstresoc /12 --> not coded in CHES but seems safe to code as radical left
	replace chesfamilyvoted="tan" if vote==13			// fremskridtspartiet /13
	replace chesfamilyvoted="rad left" if vote==14			// enhedslisten /14

	gen chesfamilyevaluated="socialist" if partynumber==1 			// soc.dem / 1
	replace chesfamilyevaluated="liberal" if partynumber==2			// det radikale venstre /2 
	replace chesfamilyevaluated="cons" if partynumber==3			// konservative /3
	replace chesfamilyevaluated="liberal" if partynumber==4			// cd /4
	replace chesfamilyevaluated="" if partynumber==5				// retsforbundet /5		--> not coded by CHES
	replace chesfamilyevaluated="rad left" if partynumber==6		// socialistisk folkeparti /6 note that CHES codes the party as green in 2019
	replace chesfamilyevaluated="rad left" if partynumber==7		// kommunisterne /7 --> not coded by CHES but seems safe to code as radical left
	replace chesfamilyevaluated="christdem" if partynumber==8		// kristendemokraterne /8 --> CHES codes as confessionalist, but we merged that category with Christian-democrats for EES analyses
	replace chesfamilyevaluated="tan" if partynumber==9		// dansk folkeparti /9
	replace chesfamilyevaluated="liberal" if partynumber==10		// venstre /10
	replace chesfamilyevaluated="liberal" if partynumber==11		// liberal alliance /11
	replace chesfamilyevaluated="rad left" if partynumber==12		// venstresoc /12 --> not coded in CHES but seems safe to code as radical left
	replace chesfamilyevaluated="tan" if partynumber==13		// fremskridtspartiet /13
	replace chesfamilyevaluated="rad left" if partynumber==14		// enhedslisten /14
	
	gen party_dyadches=chesfamilyvoted+"_"+chesfamilyevaluated
	replace party_dyadches="" if chesfamilyevaluated==""
	replace party_dyadches="" if chesfamilyvoted==""
	drop if party_dyadches==""
	replace party_dyadches="" if chesfamilyevaluated==chesfamilyvoted		// code within-family dyads as missing so they are not included in the analyses
	encode party_dyadches, gen(party_dyadches_num)

	gen party_dyadches_lim=party_dyadches	
	replace party_dyadches_lim="tan_cons" if party_dyadches=="cons_tan" 
	replace party_dyadches_lim="tan_liberal" if party_dyadches=="liberal_tan"
	replace party_dyadches_lim="tan_christdem" if party_dyadches=="christdem_tan" 
	replace party_dyadches_lim="tan_socialist" if party_dyadches=="socialist_tan"
	replace party_dyadches_lim="tan_rad left" if party_dyadches=="rad left_tan"
	replace party_dyadches_lim="tan_green" if party_dyadches=="green_tan"
	replace party_dyadches_lim="green_cons" if party_dyadches=="cons_green"
	replace party_dyadches_lim="green_liberal" if party_dyadches=="liberal_green"
	replace party_dyadches_lim="green_christdem" if party_dyadches=="christdem_green"
	replace party_dyadches_lim="green_socialist" if party_dyadches=="socialist_green"
	replace party_dyadches_lim="green_rad left" if party_dyadches=="rad left_green"
	replace party_dyadches_lim="christdem_cons" if party_dyadches=="cons_christdem"
	replace party_dyadches_lim="christdem_liberal" if party_dyadches=="liberal_christdem"
	replace party_dyadches_lim="christdem_socialist" if party_dyadches=="socialist_christdem"
	replace party_dyadches_lim="christdem_rad left" if party_dyadches=="rad left_christdem"
	replace party_dyadches_lim="cons_liberal" if party_dyadches=="liberal_cons"
	replace party_dyadches_lim="cons_socialist" if party_dyadches=="socialist_cons"
	replace party_dyadches_lim="cons_rad left" if party_dyadches=="rad left_cons"
	replace party_dyadches_lim="liberal_rad left" if party_dyadches=="rad left_liberal"
	replace party_dyadches_lim="liberal_socialist" if party_dyadches=="socialist_liberal"
	replace party_dyadches_lim="rad left_socialist" if party_dyadches=="socialist_rad left"
	
	encode party_dyadches_lim, gen(party_dyadches_lim_num)
	tab party_dyadches_lim
		
	** perceptions of distance on different dimensions
	recode v248-v325 (7=.) (999=.) (9=.) (8=.) 
	replace v263=. if v263>=6 	// some coding error in the dataset, only keep low values
	
	gen eco_perceived=abs(v248-v257) if partynumber==1 		// soc.dem
	replace eco_perceived=abs(v249-v257) if partynumber==2	// rad
	replace eco_perceived=abs(v250-v257) if partynumber==3	// kons
	replace eco_perceived=abs(v251-v257) if partynumber==4	// cd
	replace eco_perceived=abs(v252-v257) if partynumber==6	// sf
	replace eco_perceived=abs(v253-v257) if partynumber==8	// krist
	replace eco_perceived=abs(v254-v257) if partynumber==10	// venstre
	replace eco_perceived=abs(v255-v257) if partynumber==13	// frem
	replace eco_perceived=abs(v256-v257) if partynumber==14	// enhed.
	
	gen publicsector_perceived=abs(v258-v269) if partynumber==1 // soc.dem
	replace publicsector_perceived=abs(v259-v269) if partynumber==2	// rad
	replace publicsector_perceived=abs(v260-v269) if partynumber==3	// kons
	replace publicsector_perceived=abs(v268-v269) if partynumber==4	// cd
	replace publicsector_perceived=abs(v261-v269) if partynumber==6	// sf
	replace publicsector_perceived=abs(v266-v269) if partynumber==8	// krist
	replace publicsector_perceived=abs(v264-v269) if partynumber==10	// venstre
	replace publicsector_perceived=abs(v267-v269) if partynumber==13	// frem
	replace publicsector_perceived=abs(v265-v269) if partynumber==14	// enhed.
	replace publicsector_perceived=abs(v262-v269) if partynumber==11	// lib. all
	replace publicsector_perceived=abs(v263-v269) if partynumber==9		// df 
	
	gen inequality_perceived=(v270-v278) if partynumber==1 			// soc.dem
	replace inequality_perceived=abs(v271-v278) if partynumber==2	// rad
	replace inequality_perceived=abs(v272-v278) if partynumber==3	// kons
	*replace inequality_perceived=abs(v268-v278) if partynumber==4	// cd
	replace inequality_perceived=abs(v273-v278) if partynumber==6	// sf
	*replace inequality_perceived=abs(v266-v278) if partynumber==8	// krist
	replace inequality_perceived=abs(v276-v278) if partynumber==10	// venstre
	*replace inequality_perceived=abs(v267-v278) if partynumber==13	// frem
	replace inequality_perceived=abs(v277-v278) if partynumber==14	// enhed.
	replace inequality_perceived=abs(v274-v278) if partynumber==11	// lib. all
	replace inequality_perceived=abs(v275-v278) if partynumber==9	// df
	
	gen refugees_perceived=(v279-v290) if partynumber==1 			// soc.dem
	replace refugees_perceived=abs(v280-v290) if partynumber==2		// rad
	replace refugees_perceived=abs(v281-v290) if partynumber==3		// kons
	replace refugees_perceived=abs(v282-v290) if partynumber==6		// sf
	replace refugees_perceived=abs(v283-v290) if partynumber==11	// lib. all
	replace refugees_perceived=abs(v284-v290) if partynumber==9		// df
	replace refugees_perceived=abs(v285-v290) if partynumber==10	// venstre
	replace refugees_perceived=abs(v286-v290) if partynumber==14	// enhed.
	replace refugees_perceived=abs(v287-v290) if partynumber==13	// frem
	replace refugees_perceived=abs(v288-v290) if partynumber==4		// cd
	replace refugees_perceived=abs(v289-v290) if partynumber==8		// krist

	gen greenpolicy_perceived=(v291-v302) if partynumber==1 		// soc.dem
	replace refugees_perceived=abs(v292-v302) if partynumber==2		// rad
	replace refugees_perceived=abs(v293-v302) if partynumber==3		// kons
	replace refugees_perceived=abs(v294-v302) if partynumber==6		// sf
	replace refugees_perceived=abs(v295-v302) if partynumber==11	// lib. all
	replace refugees_perceived=abs(v296-v302) if partynumber==9		// df
	replace refugees_perceived=abs(v297-v302) if partynumber==10	// venstre
	replace refugees_perceived=abs(v298-v302) if partynumber==14	// enhed.
	replace refugees_perceived=abs(v299-v302) if partynumber==13	// frem
	replace refugees_perceived=abs(v301-v302) if partynumber==4		// cd
	replace refugees_perceived=abs(v300-v302) if partynumber==8		// krist
	
	gen laworder_perceived=(v303-v314) if partynumber==1 		// soc.dem
	replace laworder_perceived=abs(v304-v314) if partynumber==2		// rad
	replace laworder_perceived=abs(v305-v314) if partynumber==3		// kons
	replace laworder_perceived=abs(v306-v314) if partynumber==6		// sf
	replace laworder_perceived=abs(v307-v314) if partynumber==11	// lib. all
	replace laworder_perceived=abs(v308-v314) if partynumber==9		// df
	replace laworder_perceived=abs(v309-v314) if partynumber==10	// venstre
	replace laworder_perceived=abs(v310-v314) if partynumber==14	// enhed.
	replace laworder_perceived=abs(v311-v314) if partynumber==13	// frem
	replace laworder_perceived=abs(v313-v314) if partynumber==4		// cd
	replace laworder_perceived=abs(v312-v314) if partynumber==8		// krist

** analyses
	
	** Table P.3: Explaining like/dislikes with ideological distance between party voted and party evaluated
	eststo dkspatial1: vce2way reg like_party ecrile_distance galtan_distance if evaluatedparty!=votedparty , cluster(id year)
	eststo dkspatial2: vce2way reg like_party c.ecrile_distance##c.ecrile_distance c.galtan_distance##c.galtan_distance if evaluatedparty!=votedparty , cluster(id year)
	eststo dkspatial3: vce2way reg like_party c.year##c.ecrile_distance c.year##c.galtan_distance if evaluatedparty!=votedparty , cluster(id year)
	
	esttab dkspatial1 dkspatial2 dkspatial3  using table1-dk.tex, cells(b(star fmt(3)) se(par fmt(3)))  collab(none) stats(N r2 aic bic, fmt(0 3)) legend numbers nogap replace
	esttab dkspatial1 dkspatial2 dkspatial3  using table1-dk.rtf, cells(b(star fmt(3)) se(par fmt(3)))  collab(none) stats(N r2 aic bic, fmt(0 3)) legend numbers nogap replace	

	** Figure P.1: Effect of party family dyad on like/dislike rating, panel b
	eststo dkfamilyches: vce2way reg like_party b19.party_dyadches_num , cluster(id year)
coefplot dkfamilyches, base drop (_cons )  xline(0, lpattern(-)) sort(1) xsize(4) ysize(4.5) saving(dk_full_chesfam.gph, replace)

	** Figure P.1: Effect of party family dyad on like/dislike rating, panel a
	eststo dkfamilycheslim: vce2way reg like_party b5.party_dyadches_lim_num , cluster(id year)
coefplot dkfamilycheslim, base drop (_cons )  xline(0, lpattern(-)) sort(1) xsize(4) ysize(4.5) saving(dk_full_chesfamlim.gph, replace)

	** Figure P.2: Effect of party family dyad on like/dislike rating by time period
	eststo old: vce2way reg like_party b2.party_dyadches_num if year<1980, cluster(id year)
	coefplot old, base drop (_cons )  xline(0, lpattern(-)) sort(1) xsize(4) ysize(4.5) saving(dk_chesfam_pre1980.gph, replace)

	eststo mid: vce2way reg like_party b19.party_dyadches_num if year>1980 & year<2005, cluster(id year)
	coefplot mid, base drop (_cons )  xline(0, lpattern(-)) sort(1) xsize(4) ysize(4.5) saving(dk_chesfam_mid.gph, replace)

	eststo new: vce2way reg like_party b19.party_dyadches_num if year>=2005, cluster(id year)
	coefplot new, base drop (_cons )  xline(0, lpattern(-)) sort(1) xsize(4) ysize(4.5) saving(dk_chesfam_post2005.gph, replace)

	** Table P.4: Explaining like/dislikes with self-perceived distance between party voted and party evaluated
	eststo dkperceptions1: vce2way reg like_party publicsector_perceived refugees_perceived laworder_perceived , cluster(id year)
	eststo dkperceptions2: vce2way reg like_party c.year##c.publicsector_perceived c.year##c.refugees_perceived c.year##c.laworder_perceived , cluster(id year)

	esttab dkperceptions1 dkperceptions2   using table2-dk.tex, cells(b(star fmt(3)) se(par fmt(3)))  collab(none) stats(N r2 aic bic, fmt(0 3)) legend numbers nogap replace
	esttab dkperceptions1 dkperceptions2   using table2-dk.rtf, cells(b(star fmt(3)) se(par fmt(3)))  collab(none) stats(N r2 aic bic, fmt(0 3)) legend numbers nogap replace	
	
	** Figure P.3: The effect of party family and perceived distance
	eststo dkperceptions3: vce2way reg like_party b5.party_dyadches_lim_num if publicsector_perceived!=. & refugees_perceived!=. & laworder_perceived!=. , cluster(id year)	
	eststo dkperceptions4: vce2way reg like_party b5.party_dyadches_lim_num publicsector_perceived refugees_perceived laworder_perceived , cluster(id year)
	
	coefplot dkperceptions3 dkperceptions4, base drop (_cons *.country  publicsector_perceived refugees_perceived laworder_perceived)  xline(0, lpattern(-)) sort(1) legend(cols(4)) saving(DKfamily_spatial.gph, replace)








